package com.itwk.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itwk.yygh.common.result.Result;
import com.itwk.yygh.common.utils.MD5;
import com.itwk.yygh.hosp.model.hosp.HospitalSet;
import com.itwk.yygh.hosp.service.HospitalSetService;
import com.itwk.yygh.hosp.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author wangke
 * @version 1.0.0
 * @Description TODO
 * @createTime 2021年08月20日 14:49:00
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(value = "医院管理设置" ,tags = {"医院管理设置常用接口"})
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @GetMapping("/findAll")
    @ApiOperation(value = "获取所有医院设置的信息")
    public Result findAllHostpitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id逻辑删除医院设置信息")
    public Result removeHospSetById(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }


    @PostMapping("/findPageHospSet/{current}/{limit}")
    @ApiOperation(value = "条件分页查询医院设置信息")
    public Result findPageHospSet(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> hospitalSetPage = new Page<>(current, limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if(!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hoscode);
        }
        Page<HospitalSet> page = hospitalSetService.page(hospitalSetPage, queryWrapper);

        return  Result.ok(page);

    }

    @PostMapping("/saveHospitalSet")
    @ApiOperation(value="新增医院设置数据")
    public Result saveHospSet(@RequestBody(required = true) HospitalSet hospitalSet){
        Random random = new Random();
        String key = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));//设置密钥
        hospitalSet.setStatus(0); //0可用  1不可用
        hospitalSet.setSignKey(key);
        boolean flag = hospitalSetService.save(hospitalSet);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

    @GetMapping("/getHospSet/{id}")
    @ApiOperation(value = "根据id 获取设置信息")
    public Result getHospSetById(@PathVariable(required = true) Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }


    @PutMapping("/updateHospitalSet")
    @ApiOperation(value="修改设置信息")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

    @DeleteMapping("/batchRemove")
    @ApiOperation(value="批量删除设置信息")
    public Result batchRemoveHospSet(@RequestBody(required = true) List<Long> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

    //医院设置锁定和解锁
    @PutMapping("/lockHospitalSet/{id}/{status}")
    @ApiOperation(value = "医院设置锁定和解锁")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

    //发送签名密钥
    @GetMapping("/sendKey/{id}")
    @ApiOperation(value = "发送签名密钥")
    public Result sendHospitalSetKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信

        return Result.ok();

    }
}
